package tests;

import static org.junit.Assert.*;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class CDAParserTest {

	@Test
	public void test_parseXMLtoTriageTagObjekt_shouldWork() {
		String xml = new String("<?xml version=\"1.0\"?>" + 
		"<ClinicalDocument xmlns=\"urn:hl7-org:v3\" xmlns:voc=\"urn:hl7-org:v3/voc" + 
		"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:hl7-org:v3 CDA.xsd\">" +
		"<typeId root=\"2.16.840.1.113883.1.3\" extension=\"POCD_HD000040\"/>" + 
		"<id extension=\"7136c287-991d-4a9e-a9c7-4aa3d44fa2b8\" root=\"2.16.840.1.113883.9.12\"/>" + 
		"<code code=\"54094-8\" codeSystem=\"2.16.840.1.113883.6.1\" displayName=\"Emergency department Triage note\"/>" +
		"<effectiveTime value=\"20130506\"/>" +
		"<confidentialityCode code=\"N\" codeSystem=\"2.16.840.1.113883.5.25\"/>" + 
		"<versionNumber value=\"2\"/>" +
		"<recordTarget>" +
		"<patientRole>" +
		"<id extension=\"13372343\" root=\"1.2.276.0.58\"/>" +
		"<patient>" +
		"<name>" +
		"<given>John</given>" +
		"<family>Doe</family>" +
		"</name>" +
		"<administrativeGenderCode code=\"M\" codeSystem=\"2.16.840.1.113883.5.1\"/>" +
		"<birthTime value=\"19990808\"/>" +
		"</patient>" +
		"</patientRole>" +
		"</recordTarget>" +
		"<author>" +
		"<time value=\"20130506\"/>" +
		"<assignedAuthor>" +
		"<id extension=\"f51e7681-fc6b-4032-8f28-008f471fe32f\" root=\"2.16.840.1.113883.9.12\"/>" +
		"<assignedPerson>" +
		"<name>" +
      	"<given>Max</given>" +
      	"<family>Tagger</family>" +
    	"</name>" +
	  	"</assignedPerson>" +
		"</assignedAuthor>" +
		"</author>" +
		"<custodian>" +
		"<assignedCustodian>" +
		"<representedCustodianOrganization>" +
		"<id extension=\"13372345\" root=\"1.2.276.0.58\"/>" +
		"<name/>" +
	  	"</representedCustodianOrganization>" +
		"</assignedCustodian>" +
		"</custodian>" +
		"<relatedDocument typeCode=\"RPLC\">" +
		"<parentDocument>" +
	  	"<id extension=\"7136c287-991d-4a9e-a9c7-4aa3d44fa2b8\" root=\"2.16.840.1.113883.9.12\"/>" +
		"<versionNumber value=\"1\"/>" +
		"</parentDocument>" +
		"</relatedDocument>" +
		"<component>" +
		"<structuredBody>" +
		"<component>" +
		"<section>" +
	    "<title>Vitalzeichen</title>" +
		"<text>" +
    	"gehfaehigkeit: ja <br />" +
		"respiration: stabil <br />" +
    	"perfusion: kritisch  <br />" +
		"mentalerstatus: stabil <br />" +
    	"triagekategorie: minor <br />" +
    	"behandlung: undefiniert <br />" + 
      	"</text>" +
		"</section>" +
	  	"</component>" +
	  	"<component>" +
    	"<section>" +
      	"<title>Details</title>" +
      	"<text>" +
		"gps: kA <br />" +
		"lebensphase: kind <br />" +
    	"bergeinformation: Bergeschere notwendig. <br />" +
    	"hilfplatzposition: a <br />" +
    	"dringlichkeit: 1 <br />" +
    	"diagnose: a <br />" +
    	"blutdruck: 120:80 <br />" +
    	"puls: 80 <br />" +
    	"behandlungsverlauf: asdasd asdasd asd<br />" +
    	"transportbereitschaft: ja <br />" +
    	"zielkrankenhaus: aads <br />" +
    	"krankenkasse: aaa asd asd <br />" +
      	"</text>" +
      	"<component>" +
    	"<section>" +
      	"<title>Verletzungen</title>" +
      	"<text>" +
    	"Vorne_Kopf: Fraktur; Blutung; <br />" +
    	"Vorne_Hals: <br />" +
    	"Vorne_Brust: <br />" +
    	"Vorne_Bauch: <br />" +
    	"Vorne_ROberarm: Blutung;<br />" +
    	"Vorne_LOberarm: <br />" +
    	"Vorne_RUnterarm: <br />" +
    	"Vorne_LUnterarm: <br />" +
    	"Vorne_RHand: <br />" +
    	"Vorne_LHand: <br />" +
    	"Vorne_LOberschenkel: <br />" +
    	"Vorne_ROberschenkel: <br />" +
    	"Vorne_LUnterschenkel: <br />" +
    	"Vorne_RUnterschenkel: <br />" +
    	"Vorne_LFuss: <br />" +
    	"Vorne_RFuss: offener Bruch; <br />" +
    	"Hinten_Kopf: <br />" +
    	"Hinten_Hals: <br />" +
    	"Hinten_Brust: <br />" +
    	"Hinten_Bauch: <br />" +
    	"Hinten_ROberarm: <br />" +
    	"Hinten_LOberarm: <br />" +
    	"Hinten_RUnterarm: <br />" +
    	"Hinten_LUnterarm: <br />" +
    	"Hinten_RHand: <br />" +
    	"Hinten_LHand: <br />" +
    	"Hinten_LOberschenkel: <br />" +
    	"Hinten_ROberschenkel: <br />" +
    	"Hinten_LUnterschenkel: <br />" +
    	"Hinten_RUnterschenkel: <br />" +
    	"Hinten_LFuss: <br />" +
    	"Hinten_RFuss: aaa asd a<br />" +
      	"</text>" +
    	"</section>" +
		"</component>" +
    	"</section>" +
	  	"</component>" +
		"</structuredBody>" +
		"</component>" +
		"</ClinicalDocument>");
		
		DocumentBuilderFactory factory = 
				DocumentBuilderFactory.newInstance();	
		factory.setNamespaceAware(true);
		Document doc = null;
		
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			
			doc =  builder.parse(xml);
		} catch (ParserConfigurationException e) {
			fail();
		} catch (SAXException e) {
			fail();
		} catch (IOException e) {
			fail();
		}
		
		assertTrue(doc != null);
	}

}
